package cn.zhuama.org.controller;

import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.SystemDictionarytype;
import cn.zhuama.org.query.SystemDictionaryTypeQuery;
import cn.zhuama.org.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionarytype")
@Api(value = "系统字典的 API", description = "系统字典相关的 CRUD 功能")
public class SystemdictionaryTypeController {

    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过 ID 查询")
    public SystemDictionarytype findOne(@ApiParam(name = "编号", required = true) @PathVariable("id") Long id) {
        return systemDictionaryTypeService.londOne(id);
    }

    @GetMapping
    public List<SystemDictionarytype> findAll() {
        return systemDictionaryTypeService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemDictionaryTypeService.removeById(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败:" + e.getMessage());
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionarytype systemDictionarytype) {
        try {
            if (systemDictionarytype.getId() == null) {
                systemDictionaryTypeService.save(systemDictionarytype);
            } else {
                systemDictionaryTypeService.editById(systemDictionarytype);
            }
            return AjaxResult.me().setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("编辑失败:" + e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            systemDictionaryTypeService.patchDel(ids);
            return AjaxResult.me().setMessage("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }

    @PostMapping
    public PageList<SystemDictionarytype> queryPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQueryQuery) {
        PageList<SystemDictionarytype> page = systemDictionaryTypeService.queryData(systemDictionaryTypeQueryQuery);
        return page;
    }
}